import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.dnspod.v20210323 import dnspod_client, models


class Login:
    def __init__(self, secret_id=None, secret_key=None, token=None, mode="api",
                 domain_="baidu.com", sub_domain="www", recordtype="A"):
        """
        初始化登录参数
        :param secret_id: 登录ID
        :param secret_key: 登录key
        :param token: 登录token
        :param mode: 登录模式
        :param domain_: 需要操作的域名
        :param recordtype: 获取某种类型的解析记录，如 A，CNAME，NS，AAAA，显性URL，隐性URL，CAA，SPF等
        :param sub_domain: 需要操作的子域名记录(例如www)
        """
        self.recordtype = recordtype
        self.secret_id = secret_id
        self.secret_key = secret_key
        self.token = token
        self.domain = domain_
        self.sub_domain = sub_domain
        self.login = credential.Credential(secret_id=secret_id,
                                           secret_key=secret_key,
                                           token=token)

    def get_record(self):
        """
        获取当前域名所有记录值
        :return: 返回解析列表
        """
        try:
            httpProfile = HttpProfile()
            httpProfile.endpoint = "dnspod.tencentcloudapi.com"

            clientProfile = ClientProfile()
            clientProfile.httpProfile = httpProfile
            client = dnspod_client.DnspodClient(self.login, "", clientProfile)

            req = models.DescribeRecordListRequest()
            params = {
                "Domain": self.domain,
                "Subdomain": self.sub_domain,
                "RecordType": self.recordtype
            }
            req.from_json_string(json.dumps(params))

            resp = client.DescribeRecordList(req)
            info = resp.to_json_string()
            json_str = json.loads(str(info))
            return json_str

        except TencentCloudSDKException as err:
            print(err)
            return False

    def update(self, ip, record_id, record_line):
        """
        更新DNS记录
        :param record_line: 记录线路名称，通过 API 记录线路获得，中文，比如：默认。
        :param record_id:记录ID，通过 API 记录线路获得,整数
        :param ip:  需要更新的IP
        :return:
        """
        try:
            cred = self.login
            httpProfile = HttpProfile()
            httpProfile.endpoint = "dnspod.tencentcloudapi.com"

            clientProfile = ClientProfile()
            clientProfile.httpProfile = httpProfile
            client = dnspod_client.DnspodClient(cred, "", clientProfile)

            req = models.ModifyRecordRequest()
            params = {
                "Domain": self.domain,
                "SubDomain": self.sub_domain,
                "RecordType": "A",
                "RecordLine": record_line,
                "RecordId": record_id,
                "Value": ip,
                "TTL": 600
            }
            req.from_json_string(json.dumps(params))

            resp = client.ModifyRecord(req)
            print(resp.to_json_string())
            return True

        except TencentCloudSDKException as err:
            print(err)
            return False
